<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');  

/**
 * Controller for Subscriptions API.
 * @package api/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Subscription_api extends REST_Controller
{
    /*
     * Constructor Method
     */
    public function __construct() {
        parent::__construct();
        
        $this->load->model('subscription_model');
        
        $this->load->helper('api');
        $this->load->helper('subscription');
        
        $this->load->library('form_validation');
    }
    
    public function index_get()
    {
        $params = get_params();

        $subscriptions = $this->subscription_model->all_api($params['args'], $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order'])->get();
        response_subscriptions($subscriptions);
    }
    
    public function index_post()
    {
        if( valid_add_subscription() )
        {
            $subscription = add_subscription();
            // if successful
            if( $subscription['id'] )
            {
                format_response('success', 201, 'Add Success', NULL, $subscription);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    
    public function index_put()
    {
        put_params();
        
        if( valid_edit_subscription() )
        {
            $subscription = edit_subscription();
            // if successful
            if( $subscription )
            {
                format_response('success', 200, 'Edit Success', NULL, $subscription);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }
    
    public function index_delete()
    {
        delete_params();
        
        if( valid_delete_subscription() )
        {
            $deleted = delete_subscription();
            if( $deleted === 'deleted' OR $deleted === 'archived' )
            {
                format_response('success', 200, 'Delete Success');
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    public function subscription_by_id_get()
    {
        $params = get_params(4);

        $id = $this->uri->segment(3);
        
        $args = $params['args'];
        $args['id'] = $id;

        $subscriptions = $this->subscription_model->all_api($args, $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order'])->get();
        response_subscriptions($subscriptions);
    }
}

/* End of file subscriptions.php */
/* Location: ./application/subscriptions/backend-subscriptions/controllers/subscriptions.php */